package com.feeyin.radar.crm.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.ContractInvoiceDAO;
import com.feeyin.radar.crm.form.InvoiceForm;
import com.feeyin.radar.crm.form.InvoiceNewForm;
import com.feeyin.radar.crm.model.Invoice;
import com.feeyin.radar.crm.model.InvoiceCase;
import com.feeyin.radar.crm.service.ContractInvoiceService;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Users;

public class ContractInvoiceServiceImpl
  implements ContractInvoiceService
{
  private ContractInvoiceDAO contractInvoiceDAO;
  private UserDAO userDAO;

  public ContractInvoiceDAO getContractInvoiceDAO()
  {
    return this.contractInvoiceDAO;
  }

  public void setContractInvoiceDAO(ContractInvoiceDAO contractInvoiceDAO) {
    this.contractInvoiceDAO = contractInvoiceDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public List getInvoices(Integer contractId) {
    if (contractId == null) return null;
    List invoices = new ArrayList();
    invoices = this.contractInvoiceDAO.getInvoices(contractId);
    if (invoices != null) {
      for (int i = 0; i < invoices.size(); i++) {
        Invoice invoice = new Invoice();
        invoice = (Invoice)invoices.get(i);
        Users user = new Users();
        user = this.userDAO.getUser(invoice.getInvoicePersonId());
        if (user != null) {
          invoice.setInvoicePerson(user.getUserName());
        }
      }
    }
    return invoices;
  }

  public Date getMinInvoiceDate(Integer contractId) {
    return this.contractInvoiceDAO.getMinInvoiceDate(contractId);
  }

  public BigDecimal getTotalInvoiceCost(Integer contractId) {
    return this.contractInvoiceDAO.getTotalInvoiceCost(contractId);
  }

  public boolean insertInvoice(InvoiceForm invoiceForm) {
    if (invoiceForm == null) return false;
    if (invoiceForm.getContractId() == null) return false;
    if ((invoiceForm.getInvoiceCost() == null) || (invoiceForm.getInvoiceCost().length <= 0)) return false;
    if ((invoiceForm.getInvoiceDate() == null) || (invoiceForm.getInvoiceDesc().length <= 0)) return false;
    if ((invoiceForm.getInvoicePersonId() == null) || (invoiceForm.getInvoicePersonId().length <= 0)) return false;

    for (int i = 0; i < invoiceForm.getInvoiceCost().length; i++) {
      if ((invoiceForm.getInvoiceCost()[i] == null) || (invoiceForm.getInvoiceCost()[i].compareTo(new BigDecimal(0)) <= 0) || 
        (invoiceForm.getInvoiceDate()[i] == null) || (invoiceForm.getInvoicePersonId()[i] == null)) continue;
      Invoice invoice = new Invoice();
      invoice.setContractId(invoiceForm.getContractId());
      invoice.setCreateDate(new Date());
      invoice.setInvoiceCode(new Long(new Date().getTime() + i).toString());
      invoice.setInvoiceCost((invoiceForm.getInvoiceCost()[i]).doubleValue());
      invoice.setInvoiceDate(invoiceForm.getInvoiceDate()[i]);
      invoice.setInvoicePersonId(invoiceForm.getInvoicePersonId()[i]);
      String invoiceDesc = "";
      if ((invoiceForm.getInvoiceDesc() != null) && (invoiceForm.getInvoiceDesc().length > 0) && 
        (!StringUtils.isEmpty(invoiceForm.getInvoiceDesc()[i]))) {
        invoiceDesc = invoiceForm.getInvoiceDesc()[i];
      }
      invoice.setInvoiceDesc(invoiceDesc);
      this.contractInvoiceDAO.insertInvoice(invoice);
    }

    return true;
  }
  
  public boolean insertInvoiceNew(InvoiceNewForm invoiceNewForm)
  {
	  Invoice invoice = new Invoice();
	  invoice.setInvoiceDate(invoiceNewForm.getInvoiceDate());
	  invoice.setInvoiceCode(invoiceNewForm.getInvoiceCode());
	  invoice.setInvoiceCost(invoiceNewForm.getInvoiceTotal());
	  invoice.setInvoiceDesc(invoiceNewForm.getInvoiceDesc());
	  invoice.setInvoicePersonId(invoiceNewForm.getInvoicePersonId());
	  invoice.setContractId(invoiceNewForm.getContractId());
	  invoice.setCreateDate(new Date());
	  
	  List<InvoiceCase> invoiceCaseList = new ArrayList<InvoiceCase>();
	  String officialCostIds = invoiceNewForm.getOfficialCostIds();
	  if(officialCostIds != null && !("").equals(officialCostIds))
	  {
		  String[] officialCostIdAry = officialCostIds.split("/");
		  for(int i=0; i<officialCostIdAry.length; i++)
		  {
			  InvoiceCase ic = new InvoiceCase();
			  ic.setContractCaseId(Integer.parseInt(officialCostIdAry[i]));
			  ic.setCostType("officialCost");
			  invoiceCaseList.add(ic);
		  }		  
	  }
	  
	  String operationCostIds = invoiceNewForm.getOperationCostIds();
	  if(operationCostIds != null && !("").equals(operationCostIds))
	  {
		  String[] operationCostIdAry = operationCostIds.split("/");
		  for(int i=0; i<operationCostIdAry.length; i++)
		  {
			  InvoiceCase ic = new InvoiceCase();
			  ic.setContractCaseId(Integer.parseInt(operationCostIdAry[i]));
			  ic.setCostType("operationCost");
			  invoiceCaseList.add(ic);
		  }		  
	  }	  
	  
	  String proxyCostIds = invoiceNewForm.getProxyCostIds();
	  if(proxyCostIds != null && !("").equals(proxyCostIds))
	  {
		  String[] proxyCostIdAry = proxyCostIds.split("/");
		  for(int i=0; i<proxyCostIdAry.length; i++)
		  {
			  InvoiceCase ic = new InvoiceCase();
			  ic.setContractCaseId(Integer.parseInt(proxyCostIdAry[i]));
			  ic.setCostType("proxyCost");
			  invoiceCaseList.add(ic);
		  }		  
	  }	 	  
		  
	  invoice.setInvoiceCaseList(invoiceCaseList);
	  this.contractInvoiceDAO.insertInvoice(invoice);
	  
	  return true;
  }
  
  public Invoice getInvoiceById(Integer id)
  {
    return contractInvoiceDAO.getInvoiceById(id);
  }   
}                                                                                                                                    